<template>
    <page-container title="账号安全">
        <el-form 
        ref="formRef"
        :rules="rules"
        :model="form" 
        label-width="120px" 
        style="display: flex; flex-direction: column; align-items: center;margin-top: 120px;">
            <el-form-item label="旧密码">
                <el-input v-model="form.oldPassword" type="password" placeholder="请输入旧密码" />
            </el-form-item>
            <el-form-item label="新密码">
                <el-input v-model="form.newPassword" type="password" placeholder="请输入新密码" />
            </el-form-item>
            <el-form-item label="确认密码">
                <el-input v-model="form.confirmPassword" type="password" placeholder="请确认新密码"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleUpdatePassword">修改密码</el-button>
            </el-form-item>
        </el-form>
    </page-container>
</template>
<script setup>
import { ref } from 'vue'
import {useAdminStore} from '@/store/modules/admin'
import { useRouter } from 'vue-router'
import PageContainer from '@/components/PageContainer.vue'
import{getAdminByAdminId,updateAdminStatus} from '@/api/admin'
import { ElMessage } from 'element-plus'
import { md5 } from 'js-md5'
const router = useRouter()
const adminStore = useAdminStore()
const form = ref({
    oldPassword: '',
    newPassword: '',
    confirmPassword: ''
})
const admin = JSON.parse(localStorage.getItem('admin'))
const formRef = ref(null)
const rules = {
    oldPassword: [
    { required: true, message: '请输入旧密码', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请输入新密码密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}
const handleUpdatePassword = async () => {
    console.log(md5(form.value.oldPassword))
    await formRef.value.validate(async (valid) => {
        if (valid) {
            const res=await getAdminByAdminId(admin.adminId)
            console.log(res)
            if(res.data.password===md5(form.value.oldPassword)){
                const res=await updateAdminStatus({
                    adminId:JSON.parse(localStorage.getItem('admin')).adminId,
                    password:form.value.newPassword
                })
                if(res.code===200){
                    setTimeout(()=>{
                        localStorage.removeItem('admin')
                        localStorage.removeItem('token')
                        adminStore.removeAdminInfo()
                        adminStore.removeToken()
                        ElMessage.warning('修改密码成功，请重新登录')
                        router.push('/admin/login')
                    },1000)
                }
            }
            else{
                ElMessage.error('旧密码错误')
            }
        }
    })
}
</script>

<style scoped>

</style>
